package com.fili.gui.dialogs;

import com.fili.core.keygen.EncodedPermission;
import com.fili.core.keygen.KeyEncoder;
import com.fili.core.params.DryParams;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Oct 24, 2006
 * Time: 7:13:37 PM
 * Revision: 0
 */
public class RegisterDialog extends JDialog implements ActionListener, DocumentListener
{
    private JTextField edName;
    private JTextField edKey;
    private JButton btnOK;
    private JButton btnCancel;

    private EncodedPermission encodedPermission;

    public RegisterDialog(Frame owner)
    {
        super(owner, "Inregistrare", true);
        setupStuff();
    }


    public RegisterDialog(Dialog owner)
    {
        super(owner, "Inregistrare",true);
        setupStuff();
    }

    private void setupStuff()
    {
        setSize(270, 120);
        setLayout(null);
        setResizable(false);

        JLabel lbl = new JLabel("Nume:");
        lbl.setBounds(10, 14, 60, 13);
        add(lbl);

        edName = new JTextField();
        edName.setBounds(50, 10, 200, 20);
        edName.getDocument().addDocumentListener(this);
        add(edName);

        lbl = new JLabel("Cheie:");
        lbl.setBounds(10, 37, 60, 13);
        add(lbl);

        edKey = new JTextField();
        edKey.setBounds(50, 33, 200, 20);
        edKey.getDocument().addDocumentListener(this);
        add(edKey);

        btnOK = new JButton("OK");
        btnOK.setBounds(105, 60, 70, 24);
        add(btnOK);
        btnOK.addActionListener(this);

        btnCancel = new JButton("Cancel");
        btnCancel.setBounds(180, 60, 70, 24);
        add(btnCancel);
        btnCancel.addActionListener(this);
        updateButtons();
        setLocationRelativeTo(null);
    }

    private void updateButtons()
    {
        btnOK.setEnabled((edKey.getText().trim().length() > 0) && (edName.getText().trim().length() > 0));
    }

    public EncodedPermission showModal()
    {
        encodedPermission = null;
        setVisible(true);
        return encodedPermission;
    }

    public void actionPerformed(ActionEvent e)
    {
        if (btnOK.equals(e.getSource()))
        {
            encodedPermission = KeyEncoder.match(edName.getText().trim(), edKey.getText().trim());
            if (encodedPermission != null)
            {
                JOptionPane.showMessageDialog(this, "Va multumim pentru inregistrare.\n\nCodul introdus va ofera posibilitatea\nmonitorizarii a " + encodedPermission.nrUsc + " uscatoare.", "Inregistrare reusita", JOptionPane.INFORMATION_MESSAGE);
                DryParams.getParams().setUserName(edName.getText().trim());
                DryParams.getParams().setUserKey(edKey.getText().trim());
                setVisible(false);
                DryParams.saveParams();
            }
            else
                JOptionPane.showMessageDialog(this, "Codul introdus este incorect", "Inregistrare esuata", JOptionPane.ERROR_MESSAGE);
        }
        if (btnCancel.equals(e.getSource()))
        {
            encodedPermission = null;
            setVisible(false);
        }
    }

    public void insertUpdate(DocumentEvent e)
    {
        updateButtons();
    }

    public void removeUpdate(DocumentEvent e)
    {
        updateButtons();
    }

    public void changedUpdate(DocumentEvent e)
    {
        updateButtons();
    }
}
